package com.asking.service;

import com.asking.dao.UserMapper;
import com.asking.domain.User;
import com.asking.vo.Result;
import com.asking.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 宋定鑫
 * @Date: 2018/10/17 15:41
 */
@Service
@Transactional
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Result add(User user) {
        try{
            User username = new User();
            username.setUsername(user.getUsername());
            List<User> usernames = userMapper.select(username);
            if (usernames.size() != 0){
                return new Result(500,"用户名重复");
            }
            User userphone = new User();
            username.setPhone(user.getPhone());
            List<User> userphones = userMapper.select(userphone);
            if (userphones.size() != 0){
                return new Result(500,"手机号重复");
            }
            User useremail = new User();
            username.setEmail(user.getEmail());
            List<User> users = userMapper.select(useremail);
            if (users.size() != 0){
                return new Result(500,"邮箱重复");
            }
            user.setMark(0);
            userMapper.insert(user);
            return new Result(200);
        }catch (Exception e){
            //TODO 测试环境
            return  new Result(500,"数据库异常");
        }

    }

    public Result updata(User user) {
       try {
           userMapper.updateByPrimaryKey(user);
           return new Result(200);
       }catch (Exception e){
           //TODO 测试环境
           // return  new Result(500,"服务器异常,请联系管理员");
           return  new Result(500,"数据库异常");

       }
    }

    public User selectByUser(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public ResultVo findAll(Integer page,Integer rows) {
        PageHelper.startPage(page,rows);// PageHelper 对紧跟着它的第一条sql语句进行分页操作
        List<User> userList = userMapper.selectAll();
        PageInfo<User> info = new PageInfo<>(userList);
        ResultVo resultVo = new ResultVo();
        resultVo.setTotal(info.getTotal());
        resultVo.setRows(info.getList());

        return resultVo;
    }

    public Result deleteidUser(Integer id) {
        try{
            userMapper.deleteByPrimaryKey(id);
            return new Result(200);
        }catch (Exception e){
            //TODO 测试环境
            return  new Result(500,"数据库异常");
        }
    }

    public User selectByPhone(String phone) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);
        User user = userMapper.selectOneByExample(example);
        return user;
    }

}
